﻿Shader "UI/UIImageGray"
{
	Properties
	{
		[PerRendererdata]_MainTex("Sprite TextTure",2D) = "white"{}
		_GrayMultiplyColor("Convert Factors",Color) = (0.299, 0.587,0.114,1)
		_GrayFactor("Gray Factor",Range(0,1)) = 1

		[HideInInspector] _StencilComp("Stencil Comparison",Float) = 8
		[HideInInspector] _Stencil("Stencil ID",Float) = 0
		[HideInInspector] _StencilOp("Stencil Operation",Float) = 0
		[HideInInspector] _StencilWriteMask("Stencil Write Mask",Float) = 255
		[HideInInspector] _StencilReadMask("Stencil Read Mask",Float) = 255
		[HideInInspector] _ColorMask("Color Mask",Float) = 15

		[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip("Use Alpha Clip",Float) = 0
	}
		SubShader
		{
			Tags {
					"Oueue" = "Transparent"
					"IgnoreProjector" = "True"
					"RenderType" = "Transparent"
					"PreviewType" = "Plane"
					"CanUseSpriteAtlas" = "True"
				  }
			Stencil{
				Ref
				[_Stencil]
				Comp[_StencilComp]
				Pass[_StencilOp]
				ReadMask[_StencilReadMask]
				WriteMask[_StencilWriteMask]
			}

			Cull Off
			Lighting Off
			ZWrite Off
			ZTest[unity_GUIZTestMode]
			Blend SrcAlpha OneMinusSrcAlpha
			ColorMask[_ColorMask]


			Pass
			{
				Name "UIImage_Static_Gray"
				CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
				#pragma target 2.0

				#include "UnityCG.cginc"
				#include "UnityUI.cginc"

				#pragma multi_compile __ UNITY_UI_ALPHACLIP 

				struct appdata_t
				{
					float4 vertex : POSITION;
					float4 color : COLOR;
					float2 texcoord : TEXCOORD0;
					UNITY_VERTEX_INPUT_INSTANCE_ID

				};

				struct v2f
				{
					float4 vertex : SV_POSITION;
					fixed4 color : COLOR;
					float2 texcoord : TEXCOORD0;
					float4 worldPosition : TEXCOORD1;
					UNITY_VERTEX_OUTPUT_STEREO
				};

				uniform sampler2D _MainTex;
				uniform float4 _GrayMultiplyColor;
				uniform fixed4 _TextureSampleAdd;
				uniform float4 _ClipRect;
				uniform float _GrayFactor;


				v2f vert(appdata_t IN)
				{
					v2f OUT;
					UNITY_SETUP_INSTANCE_ID(IN);
					UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
					OUT.worldPosition = IN.vertex;
					OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
					OUT.texcoord = IN.texcoord;
					OUT.color = IN.color;
					return OUT;
				}


				fixed4 frag(v2f IN) : SV_Target
				{
					float4 colMain = tex2D(_MainTex,IN.texcoord);
					float grayColor = colMain.r*_GrayMultiplyColor.x + colMain.g*_GrayMultiplyColor.y + colMain.b*_GrayMultiplyColor.z;
					fixed4 color = lerp(colMain,float4(grayColor,grayColor,grayColor,1.0),_GrayFactor);
					color.a = colMain.a;

					color.a *= UnityGet2DClipping(IN.worldPosition.xy,_ClipRect);

					#ifdef UNITY_UI_ALPHACLIP
					clip(color.a - 0.001);
					#endif
					return color;
				}
				ENDCG
			}
		}
}